package com.oss.topic;

import com.alibaba.fastjson.JSONArray;
import com.eova.common.Easy;
import com.eova.common.base.BaseController;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.oss.model.Topic;

import static com.jfinal.kit.Ret.ok;

public class TopicController extends BaseController {
    //删除一行数据
    public void topic_delete(){
        //通过getSelectRows()来选择多行数据
        JSONArray selectRows = getSelectRows();
        if (selectRows.size()>1){
            renderJson(Easy.fail("不可选择多行数据"));
        }else {
            //getSelectValue()来获取里面的值.
            String id = getSelectValue("id");
            Db.use("oss").deleteById("topic", id);
            //返回一个提示,成功或者失败
            renderJson(Easy.sucess());
        }
    }

    public void topic_save(){
        //获取从文中输入的值
        String inputValue = getInputValue();

        Record topic = new Record().set("ttitle",1).set("tcontents",1);

        Db.use("oss").save("topic",topic);

        renderJson(Easy.sucess());
    }

    public void topic_update(){
        //从前端获取信息
        String id = getSelectValue("id");

        String inputValue = getInputValue();

        Db.use("oss").update("update topic set ttitle = ? where id = ?",inputValue,id);

        renderJson(Easy.sucess());
    }

        //find
    public void topic_find(){

    }

    /**
     * 加一个修改所有备注按钮
     */
    public void update_all(){
        String tclickcount = getInputValue();
        String ids = getSelectValue("id");
        String[] split = ids.split(",");
        for (String id : split) {
            Record record = new Record().set("id", id).set("tclickcount", tclickcount);
            Db.use("oss").update("TOPIC", record);
        }
        renderJson(Easy.sucess());
    }

    public void test(){
        String ids = getSelectValue("id");
        String[] split = ids.split(",");
        for (String id : split) {
            Topic topic = Topic.dao.findById(id);
            String tclickcount = getInputValue();
            topic.set("tclickcount",tclickcount);
            topic.update();
        }
        renderJson(Easy.info("弹出一个提示消息"));
    }



}
